package com.n1global.serveranalytics;

import com.n1global.acc.CouchDb;
import com.n1global.acc.CouchDbConfig;
import com.n1global.acc.annotation.JsView;
import com.n1global.acc.view.CouchDbMapReduceView;
import com.ning.http.client.AsyncHttpClient;

public class LogDb extends CouchDb {
    @JsView(map = "emit(doc.date.concat(doc.fields), {sum : doc.summedValue, payload : doc.payload})",
            reduce = "var sum = 0; var count = 0; for (var i = 0; i < values.length; i++) {sum += values[i].sum; if (values[i].count) count += values[i].count;} return {sum : sum, count : count};")
    private CouchDbMapReduceView<Object[], EventInfo, Object[], ReducedEventsInfo> byDateView;

    @JsView(map = "emit(doc.fields.concat(doc.date), {sum : doc.summedValue, payload : doc.payload})",
            reduce = "var sum = 0; var count = 1; for (var i = 0; i < values.length; i++) {sum += values[i].sum; if (values[i].count) count += values[i].count;} return {sum : sum, count : count};")
    private CouchDbMapReduceView<Object[], EventInfo, Object[], ReducedEventsInfo> byFieldsView;

    public LogDb(CouchDbConfig config, AsyncHttpClient httpClient) {
        super(config, httpClient);
    }

    public CouchDbMapReduceView<Object[], EventInfo, Object[], ReducedEventsInfo> getByDateView() {
        return byDateView;
    }

    public CouchDbMapReduceView<Object[], EventInfo, Object[], ReducedEventsInfo> getByFieldsView() {
        return byFieldsView;
    }
}
